var Queue = function () {
    this._methods = [];
    this._response = null;
    this._flushed = false;
};
Queue.prototype = {
    flush:function (resp, next) {
        if (this._flushed) {
            return;
        }
        this._response = resp;
        this._flushed = true;
        if (next) {
            this.next(resp);
        } else {
            while (this._methods[0]) {
                this._methods.shift()(resp);
            }
        }
    },
    reset:function () {
        this._methods = [];
        this._response = null;
        this._flushed = false;
        this._done = null;
    },
    add:function (fn) {
        if (this._flushed) {
            fn(this._response);
        } else {
            this._methods.push(fn);
        }
        return this;
    },
    next:function (resp, next) {
        if (next) {
            this.flush(resp, next);
            return;
        }
        var _this = this;
        var fn = _this._methods.shift();
        if (fn) {
            fn(resp || this._response, function () {
                _this.next.apply(_this, arguments);
            });
        }
    },
    done:function (fn) {
        if (typeof fn == 'function') {
            this._done = fn;
        } else {
            this._done(fn);
            this.reset();
        }
    }
};
module.exports = Queue;